package com.mls.medicinereminder.controller;

import com.mls.medicinereminder.entity.AvoidWarnDetails;
import com.mls.medicinereminder.entity.Medicine;
import com.mls.medicinereminder.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;

@RestController
public class SearchController {

    @Autowired
    SearchService searchService;

    @GetMapping(value = "/search")
    public ResponseEntity<List<Medicine>> getSearchResults(@RequestParam(value = "name") String name){
        List<Medicine> results=new ArrayList<Medicine>();
        List<AvoidWarnDetails> res=new ArrayList<AvoidWarnDetails>();
        List<AvoidWarnDetails> list=new ArrayList<AvoidWarnDetails>();
        list=searchService.getAllMedicines();
//        res=searchService.getSearchResults(name,list);      //编辑距离算法
        res=searchService.mySearchResults(name,list);         //自定义算法
        for(AvoidWarnDetails result:res){
            Medicine medicine=new Medicine();
            medicine.setName(result.getMedicinename());
            medicine.setRiskLevel(result.getRiskintensity());
            medicine.setRiskPoint(result.getRiskpoint());
            medicine.setUsingSuggestion(result.getUsingsuggestion());
            results.add(medicine);
        }
        return new ResponseEntity<List<Medicine>>(results, HttpStatus.OK);
    }



}
